﻿<template>
  <div class="app-container">
    <div class="filter-title">
      <el-form label-position="right" label-width="80px">
        <el-form-item :label="$t('panel.requestPerson')+' :'">
          <el-input
            v-model="listQuery.request_person"
            style="width: 150px"
            class="filter-item"
            :placeholder="$t('panel.requestPerson')"
            @keyup.enter.native="handleFilter"
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('public.mobile')+' :'">
          <el-input
            v-model="listQuery.mobile"
            style="width: 150px"
            class="filter-item"
            :placeholder="$t('public.mobile')"
            @keyup.enter.native="handleFilter"
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('public.itoc')+' :'">
          <el-input
            v-model="listQuery.itoc_name"
            style="width: 150px"
            class="filter-item"
            :placeholder="$t('public.itoc')"
            @keyup.enter.native="handleFilter"
          ></el-input>
        </el-form-item>
        <el-button
          style="margin-left:20px"
          class="filter-item"
          type="primary"
          icon="el-icon-search"
          @click="handleFilter"
        >{{ $t('table.search') }}</el-button>
        <el-button
          style="margin-left: 10px"
          class="filter-item"
          type="primary"
          @click="handleRefresh"
        >{{ $t('table.refresh') }}</el-button>
        <div v-show="!showSearchItem" class="showDown" @click="showSearchItem = true">
          <i class="el-icon-d-arrow-right"></i>
        </div>
        <div v-show="showSearchItem" class="showUp" @click="showSearchItem = false">
          <i class="el-icon-d-arrow-left"></i>
        </div>
      </el-form>
    </div>
    <div v-show="showSearchItem" class="filter-container">
      <el-form label-position="right" label-width="80px">
        <el-form-item :label="$t('public.status')+' :'">
          <el-select
            v-model="listQuery.status"
            clearable
            style="width: 150px"
            class="filter-item"
            :placeholder="$t('public.status')"
          >
            <el-option
              v-for="item in statusOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      element-loading-text="拼命加载中"
      element-loading-spinner="el-icon-loading"
      element-loading-background="rgba(255,255,255,0.2)"
      border
      fit
      highlight-current-row
      style="width: 100%min-height:1000px"
    >
      <el-table-column fixed="left" align="center" :label="$t('table.id')" width="65">
        <template slot-scope="scope">
          <span>{{ (listQuery.page-1)*listQuery.limit+scope.$index+1 }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip :label="$t('panel.requestPerson')">
        <template slot-scope="scope">
          <span>{{ scope.row.request_person }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip :label="$t('public.mobile')">
        <template slot-scope="scope">
          <span>{{ scope.row.mobile }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip :label="$t('public.itoc')">
        <template slot-scope="scope">
          <span>{{ scope.row.itoc_name }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip :label="$t('panel.param')">
        <template slot-scope="scope">
          <span>{{ scope.row.param }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('panel.equipmentStatus')">
        <template slot-scope="scope">
          <span>{{ switchEs(scope.row.equipment_status) }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip :label="$t('public.createTime')">
        <template slot-scope="scope">
          <span>{{ scope.row.create_time }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('public.status')">
        <template slot-scope="scope">
          <span :class="statusColor(scope.row.status)">{{ statusEs(scope.row.status) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        align="center"
        :label="$t('table.actions')"
        :width="operWidth"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            @click="handleRead(scope.row)"
          >{{ $t('table.read') }}</el-button>
          <el-button
            v-if="scope.row.status === 1 && permission.disable"
            type="danger"
            size="mini"
            @click="handleDisable(scope.row)"
          >{{ $t('table.disable') }}</el-button>
          <el-button
            v-else-if="permission.disable"
            type="success"
            size="mini"
            @click="handleEnable(scope.row)"
          >{{ $t('table.enable') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination
        background
        :current-page="listQuery.page"
        :page-sizes="[10,20,30,50]"
        :page-size="listQuery.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      ></el-pagination>
    </div>
    <!-- 编辑 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="temp"
        label-position="right"
        label-width="120px"
        style="width: 400px margin-left:120px"
        size="small"
      >
        <el-form-item :label="$t('panel.requestPerson')" prop="request_person">
          <el-input v-model="temp.request_person"></el-input>
        </el-form-item>
        <el-form-item :label="$t('public.mobile')" prop="mobile">
          <el-input v-model="temp.mobile"></el-input>
        </el-form-item>
        <el-form-item :label="$t('public.itoc')" prop="itoc_id">
          <el-select
            v-model="temp.itoc_id"
            filterable
            remote
            clearable
            reserve-keyword
            placeholder="请输入关键词"
            :remote-method="getOperation"
            :loading="loading"
          >
            <el-option
              v-for="item in operationOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('panel.param')" prop="param">
          <el-input v-model="temp.param"></el-input>
        </el-form-item>
        <el-form-item
          v-if="dialogStatus=='review'"
          :label="$t('panel.equipmentStatus')"
          prop="equipment_status"
        >
          <el-radio-group v-model="temp.equipment_status">
            <el-radio :label="0">开启</el-radio>
            <el-radio :label="1">关闭</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item :label="$t('public.createTime')">
          <span>{{ temp.create_time }}</span>
        </el-form-item>
        <el-form-item v-if="dialogStatus=='review'" :label="$t('public.status')" prop="status">
          <el-radio-group v-model="temp.status">
            <el-radio :label="0">审核中</el-radio>
            <el-radio :label="1">审核通过</el-radio>
            <el-radio :label="2">未通过</el-radio>
            <el-radio :label="3">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          v-if="dialogStatus=='review' && temp.status === 2"
          :label="$t('public.remark')"
          prop="check_remark"
        >
          <el-input v-model="temp.check_remark"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">{{ $t('table.cancel') }}</el-button>
        <el-button
          v-if="dialogStatus=='create'"
          type="primary"
          :disabled="!createDisabled"
          @click="createData"
        >{{ $t('table.confirm') }}</el-button>
        <el-button
          v-else
          type="primary"
          :disabled="!updateDisabled"
          @click="updateData()"
        >{{ $t('table.confirm') }}</el-button>
      </div>
    </el-dialog>

    <!-- 查看 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormRead">
      <el-form
        :model="temp"
        label-position="right"
        label-width="40%"
        style="width: 400px margin-left:120px"
      >
        <el-form-item :label="$t('panel.requestPerson')+' :'">
          <span>{{ temp.request_person }}</span>
        </el-form-item>
        <el-form-item :label="$t('public.mobile')+' :'">
          <span>{{ temp.mobile }}</span>
        </el-form-item>
        <el-form-item :label="$t('public.itoc')+' :'">
          <span>{{ temp.itoc_name }}</span>
        </el-form-item>
        <el-form-item :label="$t('panel.param')+' :'">
          <span>{{ temp.param }}</span>
        </el-form-item>
        <el-form-item :label="$t('panel.equipmentStatus')+' :'">
          <span>{{ switchEs(temp.equipment_status) }}</span>
        </el-form-item>
        <el-form-item :label="$t('public.createTime')+' :'">
          <span>{{ temp.create_time }}</span>
        </el-form-item>
        <el-form-item :label="$t('public.status')+' :'">
          <span>{{ statusEs(temp.status) }}</span>
        </el-form-item>
        <el-form-item v-if="temp.status === 2" :label="$t('public.remark')+' :'">
          <el-input v-model="temp.check_remark"></el-input>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "CentraList",
  data() {
    return {
      showSearchItem: false,
      tableKey: 0,
      list: null,
      total: null,
      regionType: null,
      regionId: null,
      listLoading: true,
      statusOptions: [
        {
          value: 1,
          label: "正常"
        },
        {
          value: 3,
          label: "禁用"
        }
      ],
      listQuery: {
        page: 1,
        limit: 20,
        request_person: "",
        mobile: "",
        itoc_name: "",
        status: ""
      },
      temp: {
        id: undefined,
        request_person: "",
        mobile: "",
        itoc_id: "",
        itoc_name: "",
        equipment_status: "",
        param: "",
        status: "",
        check_remark: ""
      },
      dialogFormVisible: false,
      dialogFormRead: false,
      dialogStatus: "",
      textMap: {
        edit: "编辑",
        create: "添加",
        review: "审核",
        read: "查看"
      },
      rules: {
        request_person: [
          { required: true, message: "请填写申请人姓名", trigger: "blur" }
        ],
        mobile: [
          { required: true, message: "请填写联系人电话", trigger: "blur" }
        ],
        itoc_id: [
          { required: true, message: "请选择运营中心", trigger: "blur" }
        ],
        param: [{ required: true, message: "请填写设备参数", trigger: "blur" }],
        equipment_status: [
          { required: true, message: "请选择设备状态", trigger: "change" }
        ],
        status: [{ required: true, message: "请审核", trigger: "blur" }],
        check_remark: [
          { required: true, message: "请填写拒绝理由", trigger: "blur" }
        ]
      },
      createDisabled: true,
      updateDisabled: true,
      loading: false,
      operationOptions: [],
      permission: {
        disable: false
      }
    };
  },
  computed: {
    operWidth: function() {
      let itemCount = 1;
      if (this.permission.disable) {
        itemCount += 1;
      }
      return itemCount * 80 + "px";
    }
  },
  created() {
    if (this.$store.state.permission.jurisdiction === "*") {
      this.permission.disable = true;
    } else {
      const jurisdiction = this.$store.state.permission.jurisdiction.split(",");
      jurisdiction.forEach(element => {
        const id = parseInt(element);
        if (id === 173) {
          this.permission.disable = true;
        }
      });
    }
    this.regionId = this.$store.state.user.id;
    this.regionType = this.$store.state.user.regionType;
    this.getList();
  },
  methods: {
    getList() {
      this.listLoading = true;
      const data = {
        rows: this.listQuery.limit,
        page: this.listQuery.page
      };
      if (this.listQuery.request_person) {
        data["request_person"] = this.listQuery.request_person;
      }
      if (this.listQuery.mobile) {
        data["mobile"] = this.listQuery.mobile;
      }
      if (this.listQuery.itoc_name) {
        data["itoc_name"] = this.listQuery.itoc_name;
      }
      if (this.listQuery.status || this.listQuery.status === 0) {
        data["status"] = this.listQuery.status;
      }
      const centraGetListPath = {
        // 获取中央控制屏列表
        company: "STQ",
        api: "pc_Centra_getList",
        version: "1.0",
        data: data
      };
      this.socketApi.sendSock(JSON.stringify(centraGetListPath), res => {
        if (res) {
          this.socketData(res);
        }
      });
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    },
    handleModifyStatus(row) {
      this.$confirm("此操作将永久删除该中央控制屏, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          const centraDeletePath = {
            company: "STQ",
            api: "pc_Centra_deleteCentra",
            version: "1.0",
            data: {
              id: row.id
            }
          };
          this.socketApi.sendSock(JSON.stringify(centraDeletePath), res => {
            if (res) {
              this.socketData(res);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    resetTemp() {
      this.temp = {
        id: undefined,
        request_person: "",
        mobile: "",
        itoc_id: "",
        itoc_name: "",
        equipment_status: "",
        param: "",
        status: "",
        check_remark: ""
      };
    },
    handleRefresh() {
      this.listQuery = {
        page: 1,
        limit: 20,
        request_person: "",
        mobile: "",
        itoc_name: "",
        status: ""
      };
      this.getList();
    },
    handleRead(row) {
      // 打开查看弹窗
      this.temp = Object.assign({}, row); // copy obj
      this.dialogStatus = "read";
      this.dialogFormRead = true;
    },
    handleDisable(row) {
      const checkRulePath = {
        company: "STQ",
        api: "pc_Centra_editCentra",
        version: "1.0",
        data: {
          id: row.id,
          status: 3
        }
      };
      this.socketApi.sendSock(JSON.stringify(checkRulePath), res => {
        if (res) {
          this.socketData(res);
        } else {
          this.$message.error(" 启用失败 ");
        }
      });
    },
    handleEnable(row) {
      const checkRulePath = {
        company: "STQ",
        api: "pc_Centra_editCentra",
        version: "1.0",
        data: {
          id: row.id,
          status: 1
        }
      };
      this.socketApi.sendSock(JSON.stringify(checkRulePath), res => {
        if (res) {
          this.socketData(res);
        } else {
          this.$message.error(" 禁用失败 ");
        }
      });
    },
    // 商家选择
    getMerchant(query) {
      const merchantGetPath = {
        company: "STQ",
        api: "pc_merchant_getName",
        version: "1.0",
        data: {
          name: query
        }
      };
      this.socketApi.sendSock(JSON.stringify(merchantGetPath), res => {
        if (res) {
          this.socketData(res);
        }
      });
    },
    // 运营中心选择
    getOperation(query) {
      const industryIndexPath = {
        company: "STQ",
        api: "pc_itoc_getList",
        version: "1.0",
        data: {
          name: query
        }
      };
      this.socketApi.sendSock(JSON.stringify(industryIndexPath), res => {
        if (res) {
          this.socketData(res);
        }
      });
    },
    socketData(res) {
      if (res !== '{"type":"ping"}') {
        const resj = JSON.parse(res);
        if (resj.code === -1) {
          this.$message.error(resj.message);
        } else {
          if (resj.api === "pc_itoc_getList") {
            this.operationOptions = resj.data;
          } else if (resj.api === "pc_Centra_editCentra") {
            // 中央控制屏状态更改
            this.getList();
          } else if (resj.api === "pc_Centra_getList") {
            // 获取中央控制屏列表
            this.total = resj.count;
            this.list = resj.data;
            this.listLoading = false;
          } else if (resj.api === "pc_Centra_deleteCentra") {
            // 删除中央控制屏列表
            this.getList();
            this.$message({
              type: "success",
              message: "删除成功!"
            });
          }
        }
      }
    },
    statusEs(status) {
      switch (parseInt(status)) {
        case 1:
          return "正常";
        case 3:
          return "禁用";
        default:
          return status;
      }
    }
  }
};
</script>
